<?php

namespace App\Http\Controllers\Dynamic;

use App\Models\Dynamic\BloodMotion;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\ApiController;

class BloodMotionController extends ApiController
{
    /**
     * @var \App\Models\Dynamic\BloodMotion
     */
    protected $bloodMotion;

    protected $request;

    /**
     * GlucoseController constructor.
     * @param \App\Models\Dynamic\BloodMotion $bloodMotion
     * @param \Illuminate\Http\Request $request
     */
    public function __construct(BloodMotion $bloodMotion, Request $request)
    {
        parent::__construct();

        $this->bloodMotion = $bloodMotion;
        $this->request = $request;
    }


    /** 获取用户运动情况
     * @return bool|\Illuminate\Http\JsonResponse
     */
    public function getListByUIdByTime()
    {

        $this->validate($this->request, [
            'startTime' => 'string',
            'endTime' => 'string',
            'user_id' => 'required|integer',
        ]);

        $user_id = $this->request->input('user_id');
        $startTime = $this->request->input('startTime');
        $endTime = $this->request->input('endTime');


        $motion= $this->bloodMotion
            ->getListUser($user_id, $startTime, $endTime)
            ->whereIsdel(0)
            ->get()
            ->toArray();

        if (!$motion) {
            return $this->response($motion);
        }

        $newMotion = [];
        if ($motion) {
            foreach ($motion as $rk => $rv) {
                $newMotion[$rk] = array(
                    'x' => (strtotime($rv['motiontime']) + (8 * 60 * 60)) * 1000,
                    'y' => 4,
                    'attr' => $rv['motiontype'] . ':' . $rv['stepsnum'],
                    'marker' => ['symbol' => 'triangle-down']
                );
            }
        }
        return $this->response($newMotion);
    }



    /** 获取用户运动情况
     * @return bool|\Illuminate\Http\JsonResponse
     */
    public function getListByTime()
    {

        $this->validate($this->request, [
            't' => 'string',
            'user_id' => 'required|integer',
        ]);

        $user_id = $this->request->input('user_id');
        $time = $this->request->input('t', date('Y-m', time()));

        //开始,结束

        $startTime = Carbon::parse($time)->startOfMonth()->toDateTimeString();
        $endTime = Carbon::parse($time)->endOfMonth()->toDateTimeString();


        $motion= $this->bloodMotion
            ->getListUser($user_id, $startTime, $endTime)
            ->whereIsdel(0)
            ->get()
            ->toArray();

        return $this->response($motion);
    }
}
